@extends('layout.master')

@section('title','添加健康资讯')

@section('css')
    @parent
    <link href="{{ asset('plugins/bower_components/summernote/dist/summernote.css') }}" rel="stylesheet"/>
    <!-- animation CSS -->
    <link href="{{ asset('css/animate.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/icheck/skins/all.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.css') }}" rel="stylesheet">
@endsection
@section('main')
    <div class="white-box">
        <form class="form-horizontal form-material" method="post" enctype="multipart/form-data">
            <div class="row">
                <div class="col-md-6 col-sm-4">
                    <div class="form-group">
                        <label class="col-md-12">标题</label>
                        <div class="col-md-12">
                            <input type="text" name="title" id="title"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>
                <div class="col-md-2 col-sm-4">
                    <label class="control-label">用户</label>
                    <div class="example">
                        <select id="user" name="user">
                            @foreach(App\User::PostingsUsers() as $value)
                                <option value="{{ $value->uuid . '||' . $value->name }}">{{ $value->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="col-md-2 col-sm-4">
                    <label class="control-label">是否热门</label>
                    <div class="example">
                        <select id="is_hot" name="is_hot">
                            @foreach(App\HealthAdvice::$HotDisplay as $key => $value)
                                <option value="{{ $key }}">{{ $value }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="col-md-2 col-sm-4">
                    <label class="control-label">类型</label>
                    <div class="example">
                        <select id="type" name="type">
                            <option value="0">无</option>
                            @foreach(DB::table('health_advice_type')->select('id', 'type')->get() as $value)
                                <option value="{{ $value->id }}">{{ $value->type }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">封面图</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="cover0" class="dropify" name="cover"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">封面图</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="cover1" class="dropify" name="cover"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">封面图</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="cover2" class="dropify" name="cover"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <div class="row">
                        <div class="col-md-12 col-sm-12">
                            <div class="form-group">
                                <label class="col-md-12">音频标题</label>
                                <div class="col-md-12">
                                    <input type="text" name="audio_title" id="audio-title"
                                           class="form-control form-control-line" required>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12 col-sm-12">
                            <div class="form-group">
                                <label class="col-md-12">音频来源</label>
                                <div class="col-md-12">
                                    <input type="text" name="befrom" id="befrom"
                                           class="form-control form-control-line" required>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">音频</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="audio-path" class="dropify" name="audio_path"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-12 col-sm-12">
                    <div class="form-group">
                        <label class="col-md-12">视频截图<span style="color:red">##多个截图用,隔开</span></label>
                        <div class="col-md-12">
                            <input type="text" name="video_f" id="video-f"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12 col-sm-12">
                    <textarea name="contents" class="form-control" id="summernote" cols="30" rows="5"></textarea>
                </div>
            </div>

        </form>
        <div class="form-group">
            <button class="fcbtn btn btn-success btn-outline btn-1e waves-effect" id="publish">添加
            </button>
            {!! csrf_field() !!}
            <a class="fcbtn btn btn-info btn-outline btn-1e waves-effect" href="{{ url('/healthAdvice/list') }}">返回
            </a>
        </div>
    </div>

@endsection
@section('js')
    @parent
    <script>
        $(document).ready(function () {
            $('#summernote').summernote();
        });
        $('#summernote').summernote({
            height: 500,
            toolbar: [
                // [groupName, [list of button]]
                ['style', ['bold', 'italic', 'underline', 'clear']],
                ['fontsize', ['fontsize']],
                ['fontname', ['fontname']],
                ['color', ['color']],
                ['para', ['ul', 'ol', 'paragraph']],
                ['height', ['height']],
                ['insert', ['link', 'picture', 'video']],
                ['view', ['fullscreen', 'codeview', 'help']]
            ],
            lang: 'zh-CN',
            placeholder: '内容',
            dialogsFade: true,
            callbacks: {
                onImageUpload: function (files) {
                    var data = new FormData();
                    data.append("file", files[0]);
                    $.ajax({
                        data: data,
                        type: "POST",
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/upload", /*填写后台上传文件的路径*/
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (url) {/*url为上传成功后返回的图片路径*/
                            var imgNode = document.createElement('img');
                            imgNode.src = url;
                            $('#summernote').summernote('insertNode', imgNode);
                        }
                    })
                }
            }
        });
    </script>

    <script>
        $("#publish").click(function (e) {
            e.preventDefault();
            var title = $("#title").val();
            var is_hot = $("#is_hot").val();
            var user = $("#user").val();
            var contents = $("#summernote").val();
            var type_value = $("#type").val();
            var video_f = $("#video-f").val();
            var audio_title = $("#audio-title").val();
            var befrom = $("#befrom").val();
            var file0 = document.getElementById("cover0").files;
            var file1 = document.getElementById("cover1").files;
            var file2 = document.getElementById("cover2").files;
            var audio = document.getElementById("audio-path").files;


            var cover = [];
            var flag = 0;
            if (file0.length) {
                if ((file0[0].type).indexOf("image/") === -1) {
                    swal("请上传图片!");
                    return false;
                }
                cover[flag] = picajax(file0[0]);
                flag = 1;
            }

            if (file1.length) {
                if ((file1[0].type).indexOf("image/") === -1) {
                    swal("请上传图片!");
                    return false;
                }
                cover[flag] = picajax(file1[0]);
                flag = 2;
            }

            if (file2.length) {
                if ((file2[0].type).indexOf("image/") === -1) {
                    swal("请上传图片!");
                    return false;
                }
                cover[flag] = picajax(file2[0]);
            }


            swal({
                    title: "",
                    text: "确定添加么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {

                    if (audio.length && (audio[0].type).indexOf("audio/") !== -1) {
                        var audio_path = picajax(audio[0]);
                    } else {
                        audio_path = '';
                    }

                    $.ajax({

                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/healthAdvice/publish",
                        type: "POST",
                        data: {
                            title: title,
                            is_hot: is_hot,
                            user: user,
                            contents: contents,
                            cover: cover,
                            type_value: type_value,
                            video_f: video_f,
                            audio_title: audio_title,
                            befrom: befrom,
                            audio_path: audio_path
                        },
                        error: function (e) {
                            if (e.readyState === 4 && e.status === 422) {
                                for (var i in e.responseJSON) {
                                    if (e.responseJSON[i] !== 'The given data was invalid.') {
                                        err = e.responseJSON[i];
                                        for (var j in err) {
                                            toasterror(err[j][0]);
                                        }
                                    }
                                }
                            }
                        },

                        success: function (data) {
                            if (data.status !== 1) {
                                swal(data.message);
                            } else {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    window.location.href = '/healthAdvice/list';
                                });
                            }
                        }
                    })
                }
            );
        });
    </script>
@endsection